<?php
class Asignaturas  extends CI_Controller{

	function __construct()
	{
		parent::__construct();
		$this->load->model('Asignaturas_Model');
		$this->load->model('Personas_Model');
	}

	function index($offset = 0)
	{

		$this->load->library('pagination');
			
		$perpage = 5;
		$config['base_url'] = site_url() . '/Asignaturas/index/';
		$config['total_rows'] = $this->Asignaturas_Model->Listar(array('count' => true));
		$config['per_page'] = $perpage;
		$config['uri_segment'] = 3;
		$config['full_tag_open'] = '<div align="center" style="height:10px; padding:10px;">';
		$config['full_tag_close'] = '</div>';
			
		$this->pagination->initialize($config);
			
		$content['pagination'] = $this->pagination->create_links();
		$content['filas'] = $this->Asignaturas_Model->Listar(array('limit' => $perpage, 'offset' => $offset));

		$data['title'] = 'LISTADO ASIGNATURAS';
		$data['content'] = $this->load->view('Asignaturas/Listar', $content, TRUE);
		$this->load->view('Plantilla/index', $data);

	}

	function ver($id){

		$content['filas'] = $this->Asignaturas_Model->Ver(array('Id' => $id));
		$content['link_back'] =  anchor('Asignaturas/index/','VOLVER AL LISTADO',array('class'=>'atras'));

		$data['title'] = 'DETALLE ASIGNATURA';
		$data['content'] = $this->load->view('Asignaturas/Detalle', $content, TRUE);

		$this->load->view('Plantilla/index', $data);
			
	}

	function eliminar($id){

		$content['filas'] = $this->Asignaturas_Model->Eliminar(array('Id' => $id));
		redirect('Asignaturas');
			
	}

	function editar($id){

		$content['filas'] = $this->Asignaturas_Model->Ver(array('Id' => $id));
		$content['link_back'] =  anchor('Asignaturas/index/','VOLVER AL LISTADO',array('class'=>'atras'));


		$this->form_validation->set_rules('IdDocente', 'DOCENTE', 'required|max_length[20]');
		$this->form_validation->set_rules('Codigo', 'CODIGO', 'required|is_numeric|max_length[20]');
		$this->form_validation->set_rules('Ano', 'ANO', 'required|is_numeric|max_length[4]');
		$this->form_validation->set_rules('Nombre', 'NOMBRE', 'required|trim|max_length[45]');
		$this->form_validation->set_rules('Programa', 'PROGRAMA', 'required|max_length[45]');
		$this->form_validation->set_rules('Facultad', 'FACULTAD', 'required|max_length[45]');
		$this->form_validation->set_rules('PlanEestudios', 'PLAN ESTUDIOS', 'max_length[2]');
		$this->form_validation->set_rules('IdiomaExtranjero', 'IDIOMA', 'required|max_length[45]');
		$this->form_validation->set_rules('Proporcion', 'PROPORCION', 'required|max_length[3]');
			
		$this->form_validation->set_error_delimiters('<span style="color:red;" class="error">', '</span>');

		if($this->form_validation->run())
		{
			$_POST['Id'] = $id;

				
			if(empty($_POST['PlanEstudios'])) unset($_POST['PlanEstudios']);
				
			if($this->Asignaturas_Model->Editar($_POST))
			{
				//echo "Error";
				$this->session->set_flashdata('flashConfirm', 'The user has been successfully updated.');
				redirect('Asignaturas');
			}
			else
			{
				//echo "Succ";
				$this->session->set_flashdata('flashError', 'A database error has occured, please contact your administrator.');
				redirect('Asignaturas/editar');
			}
		}

		$content['personas'] = $this->Personas_Model->PersonasDropdown();
		$data['title'] = 'EDITAR ASIGNATURA: '.$id;
		$data['content'] = $this->load->view('Asignaturas/Editar_Form', $content, TRUE);

		$this->load->view('Plantilla/index', $data);
			
	}


	function insertar(){

		$content['link_back'] =  anchor('Asignaturas/index/','VOLVER AL LISTADO',array('class'=>'atras'));

		$this->form_validation->set_rules('IdDocente', 'DOCENTE', 'required|max_length[20]');
		$this->form_validation->set_rules('Codigo', 'CODIGO', 'required|is_numeric|max_length[20]');
		$this->form_validation->set_rules('Ano', 'ANO', 'required|is_numeric|max_length[4]');
		$this->form_validation->set_rules('Nombre', 'NOMBRE', 'required|trim|max_length[45]');
		$this->form_validation->set_rules('Programa', 'PROGRAMA', 'required|max_length[45]');
		$this->form_validation->set_rules('Facultad', 'FACULTAD', 'required|max_length[45]');
		$this->form_validation->set_rules('PlanEestudios', 'PLAN ESTUDIOS', 'max_length[2]');
		$this->form_validation->set_rules('IdiomaExtranjero', 'IDIOMA', 'required|max_length[45]');
		$this->form_validation->set_rules('Proporcion', 'PROPORCION', 'required|max_length[3]');
			
		$this->form_validation->set_error_delimiters('<span style="color:red;" class="error">', '</span>');
			
		if($this->form_validation->run())
		{
			unset($_POST['submit']);

			if(empty($_POST['PlanEstudios'])) unset($_POST['PlanEstudios']);

			if($this->Asignaturas_Model->Insertar($_POST))
			{
				$this->session->set_flashdata('flashConfirm', 'The user has been successfully updated.');
				redirect('Asignaturas');
			}
			else
			{
				$this->session->set_flashdata('flashError', 'A database error has occured, please contact your administrator.');
				echo "estoy dentro de la funcion de resultado fracaso";
			}
		}

		$content['personas'] = $this->Personas_Model->PersonasDropdown();
		$data['title'] = 'INSERTAR ASIGNATURA';
		$data['content'] = $this->load->view('Asignaturas/Insertar_Form', $content, TRUE);

		$this->load->view('Plantilla/index', $data);
			
	}

	function id_check($str) {

			
		if ( $this->Asignaturas_Model->Ver(array('id' => $str,'count' => true)) >  0) {
			$this->form_validation->set_message('id_check', 'El %s ya esta registrado ');
			return FALSE;
		}
		else {
			return TRUE;
		}
	}

}
/* End of file Asignaturas.php */
/* Location: ./application/controllers/Asignaturas.php */